// pages/adminLogin/adminLogin.js

const app = getApp()
const db = wx.cloud.database().collection('adminList')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name:"",
        pwd:"",
        admin:null
    },

    //获取输入账号
    getNameInput(e){
        this.setData({
            name : e.detail
        })
        var name = this.data.name
        console.log("name is : " + name) 
    },
    //获取输入密码
    getPwdInput(e){
        this.setData({
            pwd : e.detail
        })
        var pwd = this.data.pwd
        console.log("pwd is : " + pwd);
    },
    //管理员登录
    adminLog(){
        var name = this.data.name
        var pwd = this.data.pwd

        db.get({
            success:(res)=>{
                for (let i = 0; i < res.data.length; i++) {
                    if (name == res.data[i].name && pwd == res.data[i].pwd) {
                    wx.showToast({
                      title: '登录成功',
                      icon:"success"
                    }),
                    wx.setStorageSync('admin', res.data[i])
                    wx.navigateTo({
                      url: '/pages/adminHome/adminHome?name='+ name +'&no=' + pwd,
                    })
                }else{
                    wx.showToast({
                      title: '账号或密码错误',
                      icon:"error"
                    })
                }
                }
                
            },
            fail:(res)=>{
                console.log(res);
            }
        })
    },
    
})